home *** CD-ROM | disk | FTP | other *** search
/ Enter 2006 September / Enter 09 2006.iso / Internet / SpamExperts Home 1.1 / SpamExperts Home.exe / lib / spamexperts.modules / ImageOps.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-07-14  |  7.3 KB  |  261 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. import Image
  5. import operator
  6.  
  7. def _border(border):
  8.     if type(border) is type(()):
  9.         if len(border) == 2:
  10.             (left, top) = (right, bottom) = border
  11.         elif len(border) == 4:
  12.             (left, top, right, bottom) = border
  13.         
  14.     else:
  15.         left = top = right = bottom = border
  16.     return (left, top, right, bottom)
  17.  
  18.  
  19. def _color(color, mode):
  20.     if Image.isStringType(color):
  21.         import ImageColor
  22.         color = ImageColor.getcolor(color, mode)
  23.     
  24.     return color
  25.  
  26.  
  27. def _lut(image, lut):
  28.     if image.mode == 'P':
  29.         raise NotImplemented, 'mode P support coming soon'
  30.     elif image.mode in ('L', 'RGB'):
  31.         if image.mode == 'RGB' and len(lut) == 256:
  32.             lut = lut + lut + lut
  33.         
  34.         return image.point(lut)
  35.     else:
  36.         raise IOError, 'not supported for this image mode'
  37.  
  38.  
  39. def autocontrast(image, cutoff = 0, ignore = None):
  40.     '''Maximize image contrast, based on histogram'''
  41.     histogram = image.histogram()
  42.     lut = []
  43.     for layer in range(0, len(histogram), 256):
  44.         h = histogram[layer:layer + 256]
  45.         if ignore is not None:
  46.             
  47.             try:
  48.                 h[ignore] = 0
  49.             except TypeError:
  50.                 for ix in ignore:
  51.                     h[ix] = 0
  52.                 
  53.             
  54.  
  55.         None<EXCEPTION MATCH>TypeError
  56.         if cutoff:
  57.             n = 0
  58.             for ix in range(256):
  59.                 n = n + h[ix]
  60.             
  61.             cut = n * cutoff / 100
  62.             for lo in range(256):
  63.                 if cut > h[lo]:
  64.                     cut = cut - h[lo]
  65.                     h[lo] = 0
  66.                 else:
  67.                     h[lo] = h[lo] - cut
  68.                     cut = 0
  69.                 if cut <= 0:
  70.                     break
  71.                     continue
  72.             
  73.             cut = n * cutoff / 100
  74.             for hi in range(255, -1, -1):
  75.                 if cut > h[hi]:
  76.                     cut = cut - h[hi]
  77.                     h[hi] = 0
  78.                 else:
  79.                     h[hi] = h[hi] - cut
  80.                     cut = 0
  81.                 if cut <= 0:
  82.                     break
  83.                     continue
  84.             
  85.         
  86.         for lo in range(256):
  87.             if h[lo]:
  88.                 break
  89.                 continue
  90.         
  91.         for hi in range(255, -1, -1):
  92.             if h[hi]:
  93.                 break
  94.                 continue
  95.         
  96.         if hi <= lo:
  97.             lut.extend(range(256))
  98.             continue
  99.         scale = 255.0 / (hi - lo)
  100.         offset = -lo * scale
  101.         for ix in range(256):
  102.             ix = int(ix * scale + offset)
  103.             if ix < 0:
  104.                 ix = 0
  105.             elif ix > 255:
  106.                 ix = 255
  107.             
  108.             lut.append(ix)
  109.         
  110.     
  111.     return _lut(image, lut)
  112.  
  113.  
  114. def colorize(image, black, white):
  115.     '''Colorize a grayscale image'''
  116.     if not image.mode == 'L':
  117.         raise AssertionError
  118.     black = _color(black, 'RGB')
  119.     white = _color(white, 'RGB')
  120.     red = []
  121.     green = []
  122.     blue = []
  123.     for i in range(256):
  124.         red.append(black[0] + i * (white[0] - black[0]) / 255)
  125.         green.append(black[1] + i * (white[1] - black[1]) / 255)
  126.         blue.append(black[2] + i * (white[2] - black[2]) / 255)
  127.     
  128.     image = image.convert('RGB')
  129.     return _lut(image, red + green + blue)
  130.  
  131.  
  132. def crop(image, border = 0):
  133.     '''Crop border off image'''
  134.     (left, top, right, bottom) = _border(border)
  135.     return image.crop((left, top, image.size[0] - right, image.size[1] - bottom))
  136.  
  137.  
  138. def deform(image, deformer, resample = Image.BILINEAR):
  139.     '''Deform image using the given deformer'''
  140.     return image.transform(image.size, Image.MESH, deformer.getmesh(image), resample)
  141.  
  142.  
  143. def equalize(image, mask = None):
  144.     '''Equalize image histogram'''
  145.     if image.mode == 'P':
  146.         image = image.convert('RGB')
  147.     
  148.     h = image.histogram(mask)
  149.     lut = []
  150.     for b in range(0, len(h), 256):
  151.         step = reduce(operator.add, h[b:b + 256]) / 255
  152.         if step == 0:
  153.             step = 1
  154.         
  155.         n = 0
  156.         for i in range(256):
  157.             lut.append(n / step)
  158.             n = n + h[i + b]
  159.         
  160.     
  161.     return _lut(image, lut)
  162.  
  163.  
  164. def expand(image, border = 0, fill = 0):
  165.     '''Add border to image'''
  166.     (left, top, right, bottom) = _border(border)
  167.     width = left + image.size[0] + right
  168.     height = top + image.size[1] + bottom
  169.     out = Image.new(image.mode, (width, height), _color(fill, image.mode))
  170.     out.paste(image, (left, top))
  171.     return out
  172.  
  173.  
  174. def fit(image, size, method = Image.NEAREST, bleed = 0.0, centering = (0.5, 0.5)):
  175.     '''
  176.     This method returns a sized and cropped version of the image,
  177.     cropped to the aspect ratio and size that you request.
  178.     '''
  179.     if type(centering) != type([]):
  180.         centering = [
  181.             centering[0],
  182.             centering[1]]
  183.     
  184.     if centering[0] > 1.0 or centering[0] < 0.0:
  185.         centering[0] = 0.5
  186.     
  187.     if centering[1] > 1.0 or centering[1] < 0.0:
  188.         centering[1] = 0.5
  189.     
  190.     if bleed > 0.49998999999999999 or bleed < 0.0:
  191.         bleed = 0.0
  192.     
  193.     bleedPixels = (int(float(bleed) * float(image.size[0]) + 0.5), int(float(bleed) * float(image.size[1]) + 0.5))
  194.     liveArea = (bleedPixels[0], bleedPixels[1], image.size[0] - bleedPixels[0] - 1, image.size[1] - bleedPixels[1] - 1)
  195.     liveSize = (liveArea[2] - liveArea[0], liveArea[3] - liveArea[1])
  196.     liveAreaAspectRatio = float(liveSize[0]) / float(liveSize[1])
  197.     aspectRatio = float(size[0]) / float(size[1])
  198.     if liveAreaAspectRatio >= aspectRatio:
  199.         cropWidth = int(aspectRatio * float(liveSize[1]) + 0.5)
  200.         cropHeight = liveSize[1]
  201.     else:
  202.         cropWidth = liveSize[0]
  203.         cropHeight = int(float(liveSize[0]) / aspectRatio + 0.5)
  204.     leftSide = int(liveArea[0] + float(liveSize[0] - cropWidth) * centering[0])
  205.     if leftSide < 0:
  206.         leftSide = 0
  207.     
  208.     topSide = int(liveArea[1] + float(liveSize[1] - cropHeight) * centering[1])
  209.     if topSide < 0:
  210.         topSide = 0
  211.     
  212.     out = image.crop((leftSide, topSide, leftSide + cropWidth, topSide + cropHeight))
  213.     return out.resize(size, method)
  214.  
  215.  
  216. def flip(image):
  217.     '''Flip image vertically'''
  218.     return image.transpose(Image.FLIP_TOP_BOTTOM)
  219.  
  220.  
  221. def grayscale(image):
  222.     '''Convert to grayscale'''
  223.     return image.convert('L')
  224.  
  225.  
  226. def invert(image):
  227.     '''Invert image (negate)'''
  228.     lut = []
  229.     for i in range(256):
  230.         lut.append(255 - i)
  231.     
  232.     return _lut(image, lut)
  233.  
  234.  
  235. def mirror(image):
  236.     '''Flip image horizontally'''
  237.     return image.transpose(Image.FLIP_LEFT_RIGHT)
  238.  
  239.  
  240. def posterize(image, bits):
  241.     '''Reduce the number of bits per color channel'''
  242.     lut = []
  243.     mask = ~(2 ** (8 - bits) - 1)
  244.     for i in range(256):
  245.         lut.append(i & mask)
  246.     
  247.     return _lut(image, lut)
  248.  
  249.  
  250. def solarize(image, threshold = 128):
  251.     '''Invert all values above threshold'''
  252.     lut = []
  253.     for i in range(256):
  254.         if i < threshold:
  255.             lut.append(i)
  256.             continue
  257.         lut.append(255 - i)
  258.     
  259.     return _lut(image, lut)
  260.  
  261.